<script lang="ts" setup name="table-page">
import { ElCol, ElPagination, ElRow } from 'element-plus'
import { ref, toRefs } from 'vue'

const props = withDefaults(defineProps<{
    pageSize?: number
    sizes?: number[]
    total?: number
}>(), {
    pageSize: 10,
    sizes: () => [10, 20, 30, 40],
    total: 100,
})
const emits = defineEmits(['handlePage'])

const { sizes, total } = toRefs(props)

const small = ref(false)
const background = ref(false)
const disabled = ref(false)
const currentPage = ref(1)
const newPageSize = ref(10)
function handleSizeChange(size: number) {
    console.log(size, '分页器的size')
    currentPage.value = 1
    newPageSize.value = size
    emits('handlePage', { page: currentPage.value, size })
}
function handleCurrentChange(page: number) {
    emits('handlePage', { page, size: newPageSize.value })
}
</script>

<template>
    <ElRow class="table-page">
        <ElCol :span="12">
            <div>
                共{{ total }}条记录 &nbsp; 第{{ currentPage }}/{{ Math.ceil(total / newPageSize) }}页
            </div>
        </ElCol>

        <ElCol :span="12"
               flex
               flex-justify-end
        >
            <ElPagination v-model:current-page="currentPage"
                          v-model:page-size="newPageSize"
                          :page-sizes="sizes"
                          :small="small"
                          :disabled="disabled"
                          :background="background"
                          layout="prev, pager, next, sizes,jumper"
                          :total="total"
                          @size-change="handleSizeChange"
                          @current-change="handleCurrentChange"
            />
        </ElCol>
    </ElRow>
</template>

<style lang="scss" scoped>
.table-page {
    margin-top: 20px;
}
</style>
